● Here's why the biometric shows "Stale" and how to fix it:

  Root Cause: The Hikvision device is at 192.168.100.161 (hospital LAN). The HMS is hosted on cPanel (YOUR-DOMAIN). The cPanel server cannot reach the local network IP — that's why sync stopped and shows "Stale".  

  Solution — Biometric Relay Agent:

  A small PHP script runs on any local PC at the hospital that can reach the Hikvision device. It pulls attendance data from the device and pushes it to the online server via HTTPS.

  Setup steps:

  1. Copy 3 files to a local PC (e.g., C:\biometric_relay\):
    - relay/biometric_relay.php — the relay agent (already configured)
    - modules/biometric/HikvisionLib.php — Hikvision communication library
    - modules/biometric/ZKTecoLib.php — (needed as dependency)
  2. Set the Hikvision password in biometric_relay.php line:
  'auth_password' => '',  // ← Set the actual Hikvision admin password here
  3. Test it manually from command line:
  php C:\biometric_relay\biometric_relay.php
  3. You should see: "Fetched X attendance records, X users from device"
  4. Schedule it to run every 10 minutes via Windows Task Scheduler:
  schtasks /create /sc minute /mo 10 /tn "BiometricRelay" /tr "C:\xampp\php\php.exe C:\biometric_relay\biometric_relay.php"

  What's pre-configured:
  - Server URL: https://YOUR-DOMAIN/api/biometric_push.php
  - API Key: <API KEY — from biometric_devices.serial_number> (set on production)
  - Device: Hikvision at 192.168.100.161:80
  - Auth: admin (password needs to be filled in)

  Once the relay runs, the device status will change from "Stale" to "Online" and attendance data will flow into the system.



● Run these commands on the local hospital PC (the one connected to the same network as the Hikvision device):

  Step 1: Create the folder and copy files
  mkdir C:\biometric_relay
  copy "C:\xampp\htdocs\afyamed\relay\biometric_relay.php" "C:\biometric_relay\"
  copy "C:\xampp\htdocs\afyamed\modules\biometric\HikvisionLib.php" "C:\biometric_relay\"
  copy "C:\xampp\htdocs\afyamed\modules\biometric\ZKTecoLib.php" "C:\biometric_relay\"

  Step 2: Set the Hikvision password — open the config file and fill in the password:
  notepad C:\biometric_relay\biometric_relay.php
  Find 'auth_password' => '', and change to 'auth_password' => 'YourHikvisionPassword',

  Step 3: Test it
  C:\xampp\php\php.exe C:\biometric_relay\biometric_relay.php
  You should see output like:
  [2026-04-02 17:50:00] === Biometric Relay Started ===
  [2026-04-02 17:50:01] Connecting to hikvision device at 192.168.100.161:80
  [2026-04-02 17:50:02] Fetched 45 attendance records, 30 users from device
  [2026-04-02 17:50:03] Filtered to 45 new records
  [2026-04-02 17:50:04] Batch 1/1: sent 45, new: 45
  [2026-04-02 17:50:04] Heartbeat OK
  [2026-04-02 17:50:04] === Biometric Relay Completed ===

  Step 4: Schedule to run every 10 minutes automatically
  schtasks /create /sc minute /mo 10 /tn "BiometricRelay" /tr "C:\xampp\php\php.exe C:\biometric_relay\biometric_relay.php" /ru SYSTEM

  Step 5: Verify it's scheduled
  schtasks /query /tn "BiometricRelay"

  To check the relay log later:
  type C:\biometric_relay\relay.log

  To remove the schedule if needed:
  schtasks /delete /tn "BiometricRelay" /f

  Type !  followed by the commands above in your terminal to run them directly. Start with Step 3 (test) to verify the connection works before scheduling.